package com.yzc.mihuyou.configurer;

import com.yzc.mihuyou.Interceptor.AuthInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.*;

import java.util.ArrayList;
import java.util.List;
@Component
@Configuration
@EnableWebMvc
public class WebConfigurer implements WebMvcConfigurer   {


    @Bean
    public AuthInterceptor authInterceptor() {
        return new AuthInterceptor();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pircevr/**").addResourceLocations("file:E:\\image\\");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        // 放行路径
        List<String> patterns = new ArrayList<>();
        patterns.add("/webjars/**");
        patterns.add("/druid/**");
        patterns.add("/pircevr/**");
        patterns.add("/swagger-resources/**");
        patterns.add("/mihuyou/user/register");
        patterns.add("/mihuyou/user/logion");
        patterns.add("/mihuyou/userimg/userImg");
        patterns.add("/mihuyou/invitation/yue/**");
        patterns.add("/mihuyou/comment/pinRue/**");
        patterns.add("/mihuyou/comimg/cunImg/**");
        patterns.add("/mihuyou/comty/cuimg/**");
        patterns.add("/mihuyou/invitation/arrIns/**");
        patterns.add("/dpircevr/**");
        patterns.add("/mihuyou/plate/**");
        patterns.add("/mihuyou/slideshow/sueArr/");
        patterns.add("/mihuyou/topic/selPae");
        patterns.add("/mihuyou/topic/topicDetail/**");
        patterns.add("/mihuyou/user/ser/**");
        patterns.add("/mihuyou/user/suePre/**");
        patterns.add("/mihuyou/invitation/suePre/**");
        patterns.add("/mihuyou/lef-img/**");
        patterns.add("/mihuyou/userimg/addImg");
        patterns.add("/mihuyou/popup/**");
        patterns.add("/mihuyou/topic/selTop");
        patterns.add("/mihuyou/user/getUser/**");
        patterns.add("/mihuyou/user/likeUser");
        patterns.add("/mihuyou/comment/selTuw/**");
        patterns.add("/mihuyou/compilations/sels/**");
//        patterns.add("/mihuyou/socket/**");
        registry.addInterceptor(authInterceptor()).addPathPatterns("/**")
                .excludePathPatterns(patterns);
    }


    @Override
    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/**")
//                .allowedOrigins("*")
                .allowedOriginPatterns("*")
                .allowedMethods("PUT", "DELETE","POST","GET","OPTIONS")
                .allowCredentials(true)
                .maxAge(3600).allowedHeaders("*");

    }
}
